<?php
namespace Util\WxSendMsg;
/**
 * mao
 * Created by PhpStorm.
 * User: mao
 * Date: 2017/3/23
 * Time: 17:05
 */
class SendMsg {

    private $APPID;
    private $SECRETID;
    private $openid;
    private $template_id;
    private $url;
    private $first;
    private $data;
    private $remark;

    /*
     *
     * 初始化 发送参数
     * $APPID 微信公共号appid
     * $SECRETID 微信公共号 SECRETID
     * $openid  发送消息模板对象用户openid
     * $template_id   消息模板id
     * $url   消息模板详情链接
     */
    public function init($APPID,$SECRETID,$openid, $template_id, $url) {
        $this->APPID = $APPID;
        $this->SECRETID = $SECRETID;
        $this->openid = $openid;
        $this->template_id = $template_id;
        $this->url = $url;
    }
    /*
     *
     * 设置 first
     */
    public function Setfirst($data, $color = '#173177') {
        $this->first = array('value' => $data, 'color' => $color);
    }
    /*
     *
     * 设置 remark
     */
    public function Setremark($data, $color = '#173177') {
        $this->remark = array('value' => $data, 'color' => $color);
    }
    /*
     *
     * 设置 消息发送体
     */
    public function Setdata($data) {
        $res['first'] = $this->first;
        for ($i = 0 ; $i < count($data) ; $i++) {
            $res['keyword' . ($i+1)] = array('value' => $data[$i], 'color' => "#173177");
        }
        $res['remark'] = $this->remark;
        $this->data = $res;
    }

    /*
 *
 * 设置 特殊消息发送体
 */
    public function Setdata1($data) {
        $res['first'] = $this->first;
        $res['order'] = array('value' => $data[0], 'color' => "#173177");
        $res['money'] = array('value' => $data[1], 'color' => "#173177");
        $res['remark'] = $this->remark;
        $this->data = $res;
    }

    /*
*
* 设置 特殊消息发送体
*/
    public function Setdata2($data) {
        $res['first'] = $this->first;
        $res['OrderSn'] = array('value' => $data[0], 'color' => "#173177");
        $res['OrderStatus'] = array('value' => $data[1], 'color' => "#173177");
        $res['remark'] = $this->remark;
        $this->data = $res;
    }

    //微信通知
    public function SendMsg() {
        //消息通知
        $template = array(
            'touser' => $this->openid,
            'template_id' => $this->template_id,
            'url' => $this->url,
            'data' =>$this->data
        );

        $SendMsg = new \Util\WxSendMsg\WechatSendMsg($this->APPID, $this->SECRETID);
        $OK = $SendMsg->SendTplMsg(json_encode($template));
        return $OK['errcode'];
    }


}